package com.xyyl.patient.service.mall;

import com.xyyl.patient.core.constant.AppEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xyyl.patient.mapper.mall.MallUserAddressMapper;
import com.xyyl.patient.entity.mall.MallUserAddress;

import java.util.List;

/**
 * @projectName:xyyl-oms-server
 * @ClassName: MallUserAddress
 * @Description:MallUserAddress
 * @author @author wqs
 * @date 2018-03-01 13:38:31
 */
@Service("mallUserAddressService")
public class MallUserAddresseServiceImp implements MallUserAddressService {

	@Autowired
	private MallUserAddressMapper mallUserAddressMapper;
	 
	
	
	public MallUserAddress queryObject(Integer id) {
		return mallUserAddressMapper.selectOne(id);
	}

	public List<MallUserAddress> queryList (MallUserAddress MallUserAddressEntity){
		return mallUserAddressMapper.selectList(MallUserAddressEntity);
	}
	@Override
	public int saveMallUserAddress(MallUserAddress MallUserAddressEntity) {
		//如果 add是设置默认地址
		if(MallUserAddressEntity.getIsDefault() ==AppEnum.common.Mall_user_address_def.getValue()){
			MallUserAddress mallUserAddress = new MallUserAddress();
			mallUserAddress.setUserId(MallUserAddressEntity.getUserId());
			mallUserAddress.setIsDefault(MallUserAddressEntity.getIsDefault());
			List<MallUserAddress>mallUserAddressList =mallUserAddressMapper.selectList(mallUserAddress);
			for (MallUserAddress addr:mallUserAddressList) {
				mallUserAddress.setId(addr.getId());
				mallUserAddress.setIsDefault(AppEnum.common.Mall_user_address_ndef.getValue());
				mallUserAddressMapper.update(mallUserAddress);
			}
		}	 
		return mallUserAddressMapper.insert(MallUserAddressEntity);
	}
	
	@Override
	public int updateMallUserAddress(MallUserAddress MallUserAddressEntity) {
		//如果 update是设置默认地址
		if(MallUserAddressEntity.getIsDefault() ==AppEnum.common.Mall_user_address_def.getValue()){
			MallUserAddress mallUserAddress = new MallUserAddress();
			mallUserAddress.setUserId(MallUserAddressEntity.getUserId());
			mallUserAddress.setIsDefault(MallUserAddressEntity.getIsDefault());
			List<MallUserAddress>mallUserAddressList =mallUserAddressMapper.selectList(mallUserAddress);
			mallUserAddressList.get(0).setIsDefault(0);
			mallUserAddressMapper.update(mallUserAddressList.get(0));
		}
		return mallUserAddressMapper.update(MallUserAddressEntity);
	}

	    
	@Override
	public int deleteMallUserAddress(Integer id) {
		return mallUserAddressMapper.delete(id);
	}

 
	    
	@Override
	public int batchDeleteMallUserAddress(Integer[] ids) {
		
		return 0;
	}
}
